package com.atguigu.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.atguigu.entity.Admin;
import com.atguigu.result.Result;
import com.atguigu.service.AdminService;
import com.atguigu.util.QiniuUtil;
import com.base.BaseController;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("admin")
public class AdminController extends BaseController {
    @Reference
    private AdminService adminService;



    @RequestMapping
    public String index(HttpServletRequest request, Model model){
        Map<String, Object> filters = getFilters(request);
        PageInfo<Admin> page = adminService.findPage(filters);
        model.addAttribute("page",page);
        model.addAttribute("filters",filters);
        return "admin/index";
    }

    @RequestMapping("/create")
    public String create(){

        return "admin/create";
    }
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, Model model){
        Admin admin = adminService.getById(id);
        model.addAttribute("admin",admin);
        return "admin/edit";
    }
    @PostMapping("/update")
    public String update(Admin admin){
        adminService.update(admin);
        return "common/successPage";
    }

    @Autowired
    private PasswordEncoder passwordEncoder;
    @PostMapping("save")
    public String save(Admin admin){
        admin.setHeadUrl("http://47.93.148.192:8080/group1/M00/03/F0/rBHu8mHqbpSAU0jVAAAgiJmKg0o148.jpg");
        admin.setPassword(passwordEncoder.encode(admin.getPassword()));
        adminService.insert(admin);
        return "common/successPage";
    }

    @GetMapping("/delete/{id}")
    public String delete(@PathVariable("id") Long id, Model model){
        adminService.delete(id);
        return "redirect:/admin";
    }

    @GetMapping("uploadShow/{id}")
    public String uploadShow(Model model,@PathVariable Long id){
        model.addAttribute("id",id);
        return "admin/upload";
    }

    @PostMapping("upload/{id}")
    public String upload(@PathVariable Long id, MultipartFile file) throws IOException {
        //上传到七牛云
        String fileName = UUID.randomUUID().toString();
        QiniuUtil.upload2Qiniu(file.getBytes(),fileName);
        Admin admin = adminService.getById(id);
        admin.setHeadUrl("http://rik6lgdtu.hb-bkt.clouddn.com/"+fileName);
        adminService.update(admin);
        return "common/successPage";
    }

    @GetMapping("assignShow/{adminId}")
    public String assignShow(@PathVariable Long adminId,Model model){
        model.addAttribute("adminId",adminId);
        Map<String,Object> map = adminService.findRoleByAdminId(adminId);
        model.addAllAttributes(map);
        return "admin/assignShow";
    }

    @PostMapping("assignRole")
    public String assignRole(@RequestParam("adminId") Long adminId,@RequestParam("roleIds") Long [] roleIds ){
        adminService.saveUserRoleRealtionShip(adminId,roleIds);
        return "common/successPage";
    }

    /**
     * 获取当前登录信息
     * @return
     */
    @GetMapping("/getInfo")
    @ResponseBody
    public Object getInfo() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();//登录用户的信息，有什么呢

        return authentication.getPrincipal();
    }

}
